package com.wangyg.juc.consumer;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wangyg6 on 2018/7/4.
 * 做面包的
 */
public class BreadMaker extends Thread {
	BlockingQueue bq;
//	List<String> list;
	AtomicInteger i = new AtomicInteger();

	public BreadMaker(String name) {
		super(name);
	}

//	public BreadMaker setList(List<String> list) {
//		this.list = list;
//		return this;
//	}

	public BreadMaker setBq(BlockingQueue bq) {
		this.bq = bq;
		return this;
	}


	public void run() {
		synchronized (bq) {
			while (bq.size() > 100) {
				try {
					bq.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			int count = i.incrementAndGet();
			System.out.println(Thread.currentThread().getName() + "生产了第" + count + "个面包");
			bq.offer(count);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			bq.notifyAll();
		}
	}
}
